package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.sc.SearchBillInfoCriteria;
import cn.buk.qms.service.BillService;
import cn.buk.tms.entity.bill.*;
import cn.buk.tms.entity.misc.OperationLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 * @author yfdai
 * @date 2016/12/3
 */
@RestController
public class BillController extends BaseController {

  @Autowired
  private BillService billService;


  /**
   * 查找账单详情
   */
  @RequestMapping("/searchBillInfoDetail.do")
  public BillInfo searchBillInfoDetail(HttpServletRequest request,
                                       @RequestParam(value = "id", defaultValue = "0") int id,
                                       @RequestParam(value = "billNum", defaultValue = "") String billNum) {
    return billService.searchBillInfoDetail(getEnterpriseId(request), id, billNum);
  }

  /**
   * 查找账单详情
   */
  @GetMapping("/bill/{id}")
  public BillInfo searchBillInfoDetail2(HttpServletRequest request,
                                       @PathVariable(value = "id") int id,
                                       @RequestParam(value = "billNum", defaultValue = "") String billNum) {
    return billService.searchBillInfoDetail(getEnterpriseId(request), id, billNum);
  }

  /**
   * 对单张账单进行销账
   * 已收款
   */
  @RequestMapping("/bill/{id}/writeOff")
  public JsonResult writeOffBill(HttpServletRequest request,
                                 @PathVariable("id") int id
  ) {
    return billService.writeOffBill(getEnterpriseId(request), getUserId(request), id);
  }

  /**
   * 已付款
   *
   * @param request
   * @param id      账单id
   * @return
   */
  @RequestMapping("/bill/{id}/paid")
  public JsonResult finishBillPayment(HttpServletRequest request,
                                      @PathVariable("id") int id
  ) {
    return billService.updateBillPaymentStatus(getEnterpriseId(request), getUserId(request), id, BillInfo.PAYMENT_MADE);
  }

  /**
   * 未付款
   *
   * @param request
   * @param id
   * @return
   */
  @RequestMapping("/bill/{id}/unpaid")
  public JsonResult undoFinishBillPayment(HttpServletRequest request,
                                          @PathVariable("id") int id
  ) {
    return billService.updateBillPaymentStatus(getEnterpriseId(request), getUserId(request), id,
            BillInfo.PAYMENT_DUE);
  }


  /**
   * 查找账单操作记录
   */
  @RequestMapping("/bill/{id}/history")
  public List<OperationLog> searchBillInfoHistory(HttpServletRequest request,
                                                  @PathVariable("id") int id
  ) {
    final int enterpriseId = getEnterpriseId(request);
    return billService.searchBillInfoHistory(enterpriseId, id);
  }

  /**
   * 修改账单客户
   */
  @PostMapping(value = "/bill/{billId}/customer")
  public JsonResult changeBillCustomer(HttpServletRequest request,
                                       @PathVariable("billId") int billId,
                                       @RequestParam("customerId") int customerId,
                                       @RequestParam("costCenter") String costCenter,
                                       @RequestParam("projectName") String projectName
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.changeBillCustomer(enterpriseId, username, billId, customerId, costCenter, projectName);
  }

  /**
   * 修改账单的供应商
   */
  @PostMapping(value = "/bill/{billId}/supplier")
  public JsonResult updateBillInfoSupplier(HttpServletRequest request,
                                           @PathVariable("billId") int billId,
                                           @RequestParam("supplierId") int supplierId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.updateBillInfoSupplier(enterpriseId, username, billId, supplierId);
  }

  /**
   * 修改账单的支付方式
   */
  @PostMapping(value = "/bill/{billId}/paymentMethod")
  public JsonResult updateBillInfoPayment(HttpServletRequest request,
                                          @PathVariable("billId") int billId,
                                          @RequestParam("paymentMethodId") int payId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.updateBillInfoPayment(enterpriseId, username, billId, payId);
  }

  /**
   * 修改账单的出票日期
   */
  @PostMapping(value = "/bill/{billId}/etdzDate")
  public JsonResult updateBillInfoEtdzDate(HttpServletRequest request,
                                           @PathVariable("billId") int billId,
                                           @RequestParam("etdzDate") Date etdzDate) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.updateBillInfoEtdzDate(enterpriseId, username, billId, etdzDate);
  }

  /**
   * 修改账单的账单日期
   */
  @PostMapping(value = "/bill/{billId}/opDate")
  public JsonResult updateBillInfoOpDate(HttpServletRequest request,
                                         @PathVariable("billId") int billId,
                                         @RequestParam("opDate") Date opDate) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.updateBillInfoOpDate(enterpriseId, username, billId, opDate);
  }

  /**
   * 修改账单的订票人
   */
  @PostMapping(value = "/bill/{billId}/op1")
  public JsonResult updateBillInfoOp1(HttpServletRequest request,
                                      @PathVariable("billId") int billId,
                                      @RequestParam("op1") String op1) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.updateBillInfoOp1(enterpriseId, username, billId, op1);
  }

  /**
   * 修改账单的备注
   */
  @PostMapping(value = "/bill/{billId}/remark")
  public JsonResult updateBillInfoRemark(HttpServletRequest request,
                                         @PathVariable("billId") int billId,
                                         @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.updateBillInfoRemark(enterpriseId, username, billId, remark);
  }

  /**
   * 修改账单的付款类型
   */
  @PostMapping(value = "/bill/{billId}/payType")
  public JsonResult updateBillInfoPayType(HttpServletRequest request,
                                          @PathVariable("billId") int billId,
                                          @RequestParam("payType") String payType) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.updateBillInfoPayType(enterpriseId, username, billId, payType);
  }


  /**
   * 分离机票账单下的某张机票
   */
  @PostMapping(value = "/bill/{billId}/flight/split/{ticketId}")
  public JsonResult splitFlightBill(HttpServletRequest request,
                                          @PathVariable("billId") int billId,
                                          @PathVariable("ticketId") int ticketId
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.splitFlightBill(enterpriseId, username, billId, ticketId);
  }

  /**
   * 分离服务账单下的某个乘机人
   */
  @PostMapping(value = "/bill/{billId}/vas/split/{orderId}")
  public JsonResult splitVasBill(HttpServletRequest request,
                                    @PathVariable("billId") int billId,
                                    @PathVariable("orderId") int orderId,
                                    @RequestParam("psgId") int psgId
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.splitVasBill(enterpriseId, username, billId, orderId, psgId);
  }

  /**
   * 查找账单金额
   *
   * @return
   */
  @RequestMapping("/searchBillSum")
  public double searchBillSum(HttpServletRequest request,
                              @RequestParam("billNum") String billNum) {
    final int enterpriseId = getEnterpriseId(request);

    return billService.searchBillSum(enterpriseId, billNum);
  }

  /**
   * 删除账单
   */
  @RequestMapping("/deleteBillInfo.do")
  public JsonResult deleteBillInfo(HttpServletRequest request,
                                   @RequestParam("id") int id) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.deleteBillInfo(enterpriseId, username, id);
  }

  /**
   * 火车票订单
   */
  @RequestMapping("/createTrainTicket.do")
  public JsonResult createTrainTicketSale(HttpServletRequest request,
                                          @RequestParam("customerId") int customerEnterpriseId,
                                          @RequestParam(value = "supplierId", defaultValue = "-1") int supplierId,
                                          @RequestParam(value = "paymentId", defaultValue = "-1") int paymentId,
                                          @RequestParam("psgName") String psgName,
                                          @RequestParam("trainNo") String trainNo,
                                          @RequestParam("dcity") String dcity,
                                          @RequestParam("acity") String acity,
                                          @RequestParam(value = "ddate", required = false) Date ddate,
                                          @RequestParam("seatClass") String seatClass,
                                          @RequestParam("price") double price,
                                          @RequestParam("serviceCharge") double serviceCharge,
                                          @RequestParam("remark") String remark
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    BillTrainTicket ticket = new BillTrainTicket();
    ticket.setEnterpriseId(enterpriseId);
    ticket.setPassengerName(psgName);
    ticket.setTrainNo(trainNo);
    ticket.setDcity(dcity);
    ticket.setAcity(acity);
    ticket.setDdate(ddate);
    ticket.setSeatClass(seatClass);
    ticket.setPrice(price);
    ticket.setServiceCharge(serviceCharge);
    ticket.setRemark(remark);

    return billService.createTrainTicket(enterpriseId, operator, customerEnterpriseId, supplierId, paymentId, ticket);
  }

  /**
   * 查找火车票销售单
   *
   * @return
   */
  @RequestMapping("/searchTrainTickets.do")
  public CommonDto<BillTrainTicket> searchTrainTickets(HttpServletRequest request,
                                                       @ModelAttribute("sc") SearchBillInfoCriteria sc,
                                                       @RequestParam(value = "customerId", defaultValue = "-1") final int customerId
  ) {
    final int enterpriseId = getEnterpriseId(request);

    sc.setCustomerId(customerId);
    List<BillTrainTicket> tickets = billService.searchTrainTickets(enterpriseId, sc);

    CommonDto<BillTrainTicket> commonDto = new CommonDto<>();
    commonDto.setDataList(tickets);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  @RequestMapping("/createHotelBooking.do")
  public JsonResult createHotelBooking(HttpServletRequest request,
                                       @RequestParam("customerId") int customerEnterpriseId,
                                       @RequestParam(value = "costCenter", required = false) String costCenter,
                                       @RequestParam(value = "projectName", required = false) String projectName,
                                       @RequestParam(value = "supplierId", defaultValue = "-1") int supplierId,
                                       @RequestParam(value = "paymentId", defaultValue = "-1") int paymentId,
                                       @RequestParam("psgName") String psgName,
                                       @RequestParam("hotelName") String hotelName,
                                       @RequestParam("roomType") String roomType,
                                       @RequestParam(value = "roomCount", defaultValue = "1") int roomCount,
                                       @RequestParam(value = "ddate", required = false) Date ddate,
                                       @RequestParam(value = "leaveDate", required = false) Date leaveDate,
                                       @RequestParam(value = "count", defaultValue = "0") int count,
                                       @RequestParam("price") double price,
                                       @RequestParam(value = "bottomPrice", defaultValue = "0") double bottomPrice,
                                       @RequestParam("serviceCharge") double serviceCharge,
                                       @RequestParam("remark") String remark,
                                       @RequestParam(value = "totalAmount", defaultValue = "0") double totalAmount,
                                       @RequestParam(value = "totalCost", defaultValue = "0") double totalCost,
                                       @RequestParam(value = "profit", defaultValue = "0") double profit
  ) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    BillHotelBooking o = new BillHotelBooking();
    o.setEnterpriseId(enterpriseId);
    o.setPassengerName(psgName);
    o.setHotelName(hotelName);
    o.setRoomType(roomType);
    o.setRoomCount(roomCount);
    o.setDdate(ddate);
    o.setLeaveDate(leaveDate);
    o.setCount(count);
    o.setPrice(price);
    o.setBottomPrice(bottomPrice);
    o.setServiceCharge(serviceCharge);
    o.setRemark(remark);
    o.setCostCenter(costCenter);
    o.setProjectName(projectName);

    o.setTotalAmount(totalAmount);
    o.setTotalCost(totalCost);
    o.setProfit(profit);


    return billService.createHotelBooking(enterpriseId, operator, customerEnterpriseId, supplierId, paymentId, o);
  }

  /**
   * 查找j酒店订单
   *
   * @return
   */
  @RequestMapping("/searchHotelBookings.do")
  public CommonDto<BillHotelBooking> searchHotelBookings(HttpServletRequest request,
                                                         @ModelAttribute("sc") SearchBillInfoCriteria sc) {
    final int enterpriseId = getEnterpriseId(request);

    List<BillHotelBooking> tickets = billService.searchHotelBookings(enterpriseId, sc);

    CommonDto<BillHotelBooking> commonDto = new CommonDto<>();
    commonDto.setDataList(tickets);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }


  /**
   * 查找已录入的已开机票,单张
   *
   * @return
   */
  @RequestMapping("/searchFlightTicket.do")
  public BillFlightTicket searchFlightTickets(HttpServletRequest request,
                                              @RequestParam("ticketNo") String ticketNo) {
    final int enterpriseId = getEnterpriseId(request);

    return billService.searchFlightTicket(enterpriseId, ticketNo);
  }

  /**
   * 账单审核通过
   */
  @PostMapping(value = "/bill/{id}/audit/passed")
  public JsonResult auditBillInfoDone(HttpServletRequest request,
                                      @PathVariable("id") int id) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return billService.auditBillInfoDone(enterpriseId, operator, id);
  }

  /**
   * 账单审核通过(使用账单号）
   */
  @PostMapping(value = "/bill/audited")
  public JsonResult auditBillInfoDoneByBillNum(HttpServletRequest request,
                                               @RequestParam("billNum") String billNum) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return billService.auditBillInfoDoneByBillNum(enterpriseId, operator, billNum);
  }

  /**
   * 设置账单为未审核, 取消审核
   */
  @PostMapping(value = "/bill/{id}/audit/canceled")
  public JsonResult cancelBillInfoAuditPassed(HttpServletRequest request,
                                              @PathVariable("id") int id,
                                              @RequestParam("remark") String remark) {
    return billService.cancelBillInfoAuditPassed(getEnterpriseId(request), getUsername(request), id, remark);
  }


  /**
   * 将账单从结算单中删除
   */
  @PostMapping(value = "/bill/{id}/settlement/removed")
  public JsonResult deleteSettlementNo(HttpServletRequest request,
                                       @PathVariable("id") int id) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return billService.deleteSettlementNo(enterpriseId, username, id);
  }

  /**
   * 创建退票账单
   */
  @RequestMapping("/createFlightRefundTicket.do")
  public JsonResult createFlightRefundTicket(HttpServletRequest request,
                                             @RequestParam("ticketNo") String ticketNo,
                                             @RequestParam("psgName") String psgName,
                                             @RequestParam("accountRecv") double accountRecv,
                                             @RequestParam("airRefundFee") double airRefundFee,
                                             @RequestParam("serviceCharge") double serviceCharge,
                                             @RequestParam("remark") String remark) {
    final int enterpriseId = getEnterpriseId(request);
    final String operator = getUsername(request);

    return billService.createFlightRefundTicket(enterpriseId, ticketNo, psgName, accountRecv, airRefundFee, serviceCharge, remark, operator);
  }


  /**
   * 把机票订单转换为账单
   */
  @RequestMapping("/convertFlightOrder2Bill")
  public JsonResult importFlightOrder(HttpServletRequest request,
                                      @RequestParam("id") int flightOrderId) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return importService.importFlightOrder(enterpriseId, username, flightOrderId);
  }

  /**
   * 把退票单转换为账单
   */
  @RequestMapping("/convertFlightRefundOrder2Bill")
  public JsonResult importFlightRefundOrder(HttpServletRequest request,
                                            @RequestParam("id") int id) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return importService.importFlightRefundOrder(enterpriseId, username, id);
  }

  /**
   * 把改签单转换为账单
   */
  @RequestMapping("/convertFlightChangeOrder2Bill")
  public JsonResult importFlightChangeOrder(HttpServletRequest request,
                                            @RequestParam("id") int id) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return importService.importFlightChangeOrder(enterpriseId, username, id);
  }

  /**
   * vasOrder转换为账单
   */
  @RequestMapping("/convertVasOrder2Bill")
  public JsonResult importVasOrder(HttpServletRequest request,
                                   @RequestParam("id") int id) {
    final int enterpriseId = getEnterpriseId(request);
    final String username = getUsername(request);

    return importService.importVasOrder(enterpriseId, username, id);
  }

  /**
   * 查找已录入的退票账单
   *
   * @return
   */
  @RequestMapping("/bill/flight/refund/tickets")
  public CommonDto<BillFlightRefundTicket> searchFlightRefundTickets(HttpServletRequest request,
                                                                     @ModelAttribute("sc") CommonSearchCriteria sc) {
    final int enterpriseId = getEnterpriseId(request);

    List<BillFlightRefundTicket> tickets = billService.searchFlightRefundTickets(enterpriseId, sc);

    CommonDto<BillFlightRefundTicket> commonDto = new CommonDto<>();
    commonDto.setDataList(tickets);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

}
